package cn.snailhd.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * properties������
 * @author hd
 *
 */
public class PropertiesManage {
	
	public static String encode = "UTF-8";
	
	/**
	 * д��Properties�е����
	 * @param filePath
	 * @param pro
	 */
	public void writeProperties(String filePath,Properties pro){
		try {
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), encode);
			pro.store(osw, "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	public void writeProperties(File filePath,Properties pro){
		try {
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), encode);
			pro.store(osw, "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	/**
	 * д������
	 * @param filePath
	 * @param parameterKey
	 * @param parameterValue
	 */
	public void writeProperties(String filePath,String parameterKey,String parameterValue){
		Properties pro =new Properties();
		try {
			//����������ȡ�����б�
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),encode);
			pro.load(isr);
			//��ֵ���뵽properties��
			pro.setProperty(parameterKey, parameterValue);
			//���±���
			OutputStreamWriter  osw = new OutputStreamWriter(new FileOutputStream(filePath), encode);
			pro.store(osw, "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	
	
	
	
	/**
	 * ��ȡ�����ļ�
	 * @param filePath
	 * @return
	 */
	public Properties readProperties(String filePath){
		Properties pro = new Properties();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath));
			pro.load(isr);
			return pro;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	public Properties readProperties(File filePath){
		Properties pro = new Properties();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath));
			pro.load(isr);
			return pro;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	
	public static void main(String[] args) {
		PropertiesManage pm =new PropertiesManage();
		
		Properties pro = new Properties();
		pro.setProperty("ac", "ab");
		pro.setProperty("bd", "bc");
		pro.setProperty("book", "read");
		pm.writeProperties("d:/abc", pro);
		
		/*pm.writeProperties("d:/abc", "bd", "bc");*/
		
		
		/*Properties pro = pm.readProperties("d:/abc");
				
		if(pro.getProperty("a")!=null){
			System.out.println("don't null");
		}else{
			System.out.println("is null");
		}	*/
		
	}
}
